package com.example.myapplication.fragmentView;

import android.util.DisplayMetrics;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import com.example.myapplication.R;

public class FragmentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        int widthPixels = outMetrics.widthPixels;
        int heightPixels = outMetrics.heightPixels;

        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragmentById = fragmentManager.findFragmentById(R.id.add_image_fragment);
        // 设置图片仅在竖屏的时候显示

        //横屏
        if (widthPixels > heightPixels) {
            if (fragmentById != null) {
                fragmentManager.beginTransaction().remove(fragmentById).commitNow();
            }
        //竖屏
        } else {
            if (fragmentById == null) {
                fragmentManager.beginTransaction().add(R.id.add_image_fragment, new ImageFragment()).commitNow();
            }
        }

    }
}